package com.wdz.service.impl;

import com.wdz.article.entity.Article;
import com.wdz.article.mapper.ArticleMapper;
import com.wdz.article_comment.entity.ArticleComment;
import com.wdz.article_comment.mapper.ArticleCommentMapper;
import com.wdz.category.entity.Category;
import com.wdz.category.mapper.CategoryMapper;
import com.wdz.currency_query.service.CurrencyQueryService;
import com.wdz.currency_query.utils.Param;
import com.wdz.service.IArticleService;
import com.wdz.utils.PageUtil;
import com.wdz.utils.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Service("ArticleServiceImpl2")
public class ArticleServiceImpl implements IArticleService {
    private CurrencyQueryService currencyQueryService = new CurrencyQueryService();

    private CategoryMapper categoryMapper;

    @Autowired
    public void setCategoryMapper(CategoryMapper categoryMapper){
        this.categoryMapper = categoryMapper;
    }

    @Resource
    private ArticleMapper articleMapper;

    //根据relation_category_ids查询出relation_category_names
    private List<Article> dgRelationCategoryIds(List<Article> datas){
        Set<String> ids = new HashSet<>();
        datas.forEach(item->{
            if(item.getRelationCategoryIds() != null)
            ids.addAll(Arrays.asList(item.getRelationCategoryIds().split(",")));
        });
        if(!ids.isEmpty()) {
            List<Category> categories = categoryMapper.selectBatchIds(ids);
            datas.forEach(item1 -> {
                item1.setRelationCategorys(dgRelationCategoryIds(item1, categories));
            });

        }
        return datas;
    }


    public Article dgRelationCategoryIds(Article article){
        if(StringUtils.isEmpty(article.getRelationCategoryIds())){
            return article;
        }
        Set<String> ids = new HashSet<>();
        ids.addAll(Arrays.asList(article.getRelationCategoryIds().split(",")));
        if(!ids.isEmpty()){
            List<Category> categories = categoryMapper.selectBatchIds(ids);
            article.setRelationCategorys(categories);
        }
        return article;
    }

    private List<Category> dgRelationCategoryIds(Article article,List<Category> categories){
        if(article.getRelationCategoryIds() == null){
            return null;
        }
        String[] ids = article.getRelationCategoryIds().split(",");
        StringBuilder names = new StringBuilder();
        List<Category> categories1 = new ArrayList<>();
        for (String id : ids) {
            categories.forEach(item->{
                if(id.equals(item.getId())) {
                    Category c = new Category();
                    c.setName(item.getName());
                    c.setId(item.getId());
                    categories1.add(c);
                    c = null;
                }
            });
        }
        return categories1;
    }

    @Override
    public List<Article> queryListByPage(Integer pageNo, Integer pageSize) {
        return a1(pageNo,pageSize,0);
    }
    @Override
    public List<Article> queryDraftListByPage(Integer pageNo, Integer pageSize) {
        return a1(pageNo,pageSize,1);
    }
    private List<Article> a1(Integer pageNo, Integer pageSize,int type){
        Param param = new Param();
        param.keywords = param.new Keywords();
        param.keywords.setK_type(3);
        Map<String,Object> m = new HashMap<>();//将置顶的优先查询出来
        m.put("isAsc","false");
        m.put("cloum","is_topping");
        param.keywords.setOrderBy(m);
        param.setPageNo(pageNo);
        param.setPageSize(pageSize);
        ParamsUtil paramsUtil = new ParamsUtil();
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        paramsUtil.setParams(map);
        param.keywords.setAdditionalMap(paramsUtil);
        PageUtil pageUtil = currencyQueryService.searchCurr("article",param);
        return dgRelationCategoryIds((List<Article>) pageUtil.getData());
    }

    @Override
    public List<Category> queryCategoryListByPage(Integer pageNo, Integer pageSize) {
        Param param = new Param();
        param.keywords = param.new Keywords();
        param.keywords.setK_type(3);
        param.setPageNo(pageNo);
        param.setPageSize(pageSize);
        Map<String,Object> orderby = new HashMap<>();
        orderby.put("cloum","create_date");
        orderby.put("isAsc","false");
        param.keywords.setOrderBy(orderby);
        PageUtil pageUtil = currencyQueryService.searchCurr("category",param);
        return (List<Category>) pageUtil.getData();
    }

    @Override
    public List<Article> queryArticlesBOLC(String id, Integer pageNo, Integer pageSize) {
        return dgRelationCategoryIds(categoryMapper.selectArticleBOLC(id,(pageNo-1)*pageSize,pageSize));
    }

    @Override
    public int selectArticleCountBOLC(String id) {
        return categoryMapper.selectArticleCountBOLC(id);
    }

    @Override
    public List<Article> likeArticles(String name, Integer pageNo, Integer pageSize) {
        Param param = new Param();
        param.keywords = param.new Keywords();
        Map<String,Object> m = new HashMap<>();//将置顶的优先查询出来
        m.put("isAsc","false");
        m.put("cloum","is_topping");
        param.keywords.setOrderBy(m);
        param.keywords.setK_type(0);
        param.keywords.setK_cloum("title");
        param.keywords.setK_value(name);
        ParamsUtil paramsUtil = new ParamsUtil();
        Map<String,Object> map = new HashMap<>();
        map.put("type",0);
        paramsUtil.setParams(map);
        param.keywords.setAdditionalMap(paramsUtil);
        param.setPageNo(pageNo);
        param.setPageSize(pageSize);

        PageUtil pageUtil = currencyQueryService.searchCurr("article",param);
        return dgRelationCategoryIds((List<Article>) pageUtil.getData());
    }

    @Override
    public int likeTitleCount(String name) {
        return articleMapper.likeTitleCount(name);
    }

    @Override
    public List<Article> likeArticlesByCate(String name,String id, Integer pageNo, Integer pageSize) {
        return dgRelationCategoryIds(categoryMapper.selectArticleBOLCLT(id,name,(pageNo-1)*pageSize, pageSize));
    }

    @Override
    public int likeTitleByCateCount(String name, String id) {
        return categoryMapper.selectArticleCountBOLCLT(id,name);
    }

    @Resource
    private ArticleCommentMapper articleCommentMapper;

    @Override
    public List<ArticleComment> queryCommentsAndReplys(String id, Integer pageNo, Integer pageSize) {
        return articleCommentMapper.getHasReplysComments2(id, (pageNo-1)*pageSize, pageSize);
    }

    @Override
    public List<Article> queryArticlesByres(String id,Integer pageNo, Integer pageSize) {
        return dgRelationCategoryIds(articleMapper.selectArticleByRes(id,(pageNo-1)*pageSize,pageSize));
    }

    @Override
    public int queryArticlesByresCount(String id) {
        return articleMapper.selectArticleByResCount(id);
    }


}
